﻿using System.Collections.Generic;

namespace FSMStateMachine
{
    public class State
    {
        #region State Field

        /// <summary>
        /// 状态名称
        /// </summary>
        private string stateName;
        /// <summary>
        /// 状态标签
        /// </summary>
        private string stateTag;
        /// <summary>
        /// 状态切换字典
        /// </summary>
        private Dictionary<string, System.Func<bool>> stateTransition;

        #endregion

        #region State Properties

        public string StateName
        {
            get
            {
                return stateName;
            }
        }


        public string StateTag
        {
            get
            {
                return stateTag;
            }
        }

        #endregion

        #region Constructor

        public State(string stateName)
        {
            this.stateName = stateName;
            stateTransition = new Dictionary<string, System.Func<bool>>();
        }

        public State(string stateName,string stateTag):this(stateName)
        {
            this.stateTag = stateTag;
        }

        #endregion

        #region State Event

        public System.Action<State> OnStateEnter;
        public System.Action<State> OnStateUpdate;
        public System.Action<State> OnStateExit;
        public System.Action<State, float> OnSpecialTime;

        /// <summary>
        /// 进入当前事件
        /// </summary>
        public void CurrentStateEnter()
        { 
            if(OnStateEnter != null)
            {
                //执行进入事件
                OnStateEnter(this);
            }
            if(OnStateUpdate != null)
            { 
                if(!UnityMonoStateHelper.instance.stateUpdate.ContainsKey(this))
                {
                    //添加Update事件到字典
                    UnityMonoStateHelper.instance.stateUpdate.Add(this,OnStateUpdate);
                }
            }
        }
        /// <summary>
        /// 离开当前事件
        /// </summary>
        public void CurrentStateExit()
        {


            if (OnStateExit != null)
            {
                //执行进入事件
                OnStateExit(this);
            }

            if (UnityMonoStateHelper.instance.stateUpdate.ContainsKey(this))
            {
                //从字典移除Update事件
                UnityMonoStateHelper.instance.stateUpdate.Remove(this);
            }
        }

        #endregion

        #region State Functions

        /// <summary>
        /// 添加目标切换或设置目标切换
        /// </summary>
        /// <param name="targetStateName">Target state name.</param>
        /// <param name="conditions">Conditions.</param>
        public void SetStateTransition(string targetStateName,System.Func<bool> conditions)
        { 
            //如果包含该目标
            if(stateTransition.ContainsKey(targetStateName))
            {
                //则添加条件
                stateTransition[targetStateName] += conditions;
                return;
            }
            //不包含则直接设置目标
            stateTransition.Add(targetStateName, conditions);
        }

        /// <summary>
        /// 获取目标切换条件
        /// </summary>
        /// <param name="targetStateName">Target state name.</param>
        public System.Func<bool> GetStateTransition(string targetStateName)
        { 
            if(stateTransition.ContainsKey(targetStateName))
            {
                return stateTransition[targetStateName];
            }

            return null;
        }

        public Dictionary<string,System.Func<bool>> GetAllStateTransition()
        {
            if (stateTransition == null)
                return null;
            return stateTransition;
        }

        #endregion
    }
}